package com.ifacebox.web.common.client;

import com.ifacebox.web.common.model.UploadProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author znn
 */
public abstract class OssClient {
    protected static final Logger logger = LoggerFactory.getLogger(OssClient.class);
    protected static final String PROTOCOL_HTTP = "http://";
    protected static final String PROTOCOL_HTTPS = "https://";
    protected UploadProperties uploadProperties;

    public OssClient(UploadProperties uploadProperties) {
        this.uploadProperties = uploadProperties;
    }

    /**
     * 上传本地文件并存储到oss
     *
     * @param file       上传文件
     * @param bucketName oss的存储空间名称
     * @param fileName   相对于本地存储目录的文件地址
     * @return oss存储文件的下载地址
     * @throws IOException
     */
    public String transferTo(MultipartFile file, String bucketName, String fileName) throws IOException {
        if (logger.isInfoEnabled()) {
            logger.info("上传文件 - {}/{}", uploadProperties.getLocation(), fileName);
        }
        file.transferTo(new File(fileName));
        if (!StringUtils.isEmpty(uploadProperties.getOss().getBucket())
                && !StringUtils.isEmpty(uploadProperties.getOss().getEndpoint())
                && !StringUtils.isEmpty(uploadProperties.getOss().getAccessKeyId())
                && !StringUtils.isEmpty(uploadProperties.getOss().getAccessKeySecret())) {
            return this.transferTo(bucketName, fileName);
        }
        return null;
    }

    /**
     * 本地文件存储到oss
     *
     * @param bucketName oss的存储空间名称
     * @param fileName   相对于本地存储目录的文件地址
     * @return oss存储文件的下载地址
     * @throws IOException
     */
    public String transferTo(String bucketName, String fileName) throws IOException {
        if (StringUtils.isEmpty(bucketName)) {
            bucketName = uploadProperties.getOss().getBucket();
        }
        if (logger.isInfoEnabled()) {
            logger.info("上传文件 - {}/{} OSS - {}", uploadProperties.getOss().getEndpoint(), fileName, bucketName);
        }
        this.upload(bucketName, fileName);
        String fileOssUrl = String.format("%s/%s", uploadProperties.getOss().getEndpoint(), fileName);
        if (uploadProperties.getOss().getEndpoint().contains(PROTOCOL_HTTP)) {
            return fileOssUrl.replace(PROTOCOL_HTTP, String.format("%s%s.", PROTOCOL_HTTP, bucketName));
        } else if (uploadProperties.getOss().getEndpoint().contains(PROTOCOL_HTTPS)) {
            return fileOssUrl.replace(PROTOCOL_HTTPS, String.format("%s%s.", PROTOCOL_HTTPS, bucketName));
        } else {
            return String.format("%s%s.%s", PROTOCOL_HTTP, bucketName, fileOssUrl);
        }
    }

    /**
     * 上传文件到OSS
     *
     * @param bucketName oss的存储空间名称
     * @param fileName   相对于本地存储目录的文件地址
     * @throws IOException
     */
    public abstract void upload(String bucketName, String fileName) throws IOException;
}
